<?php

//运程定义控制器
class ShipCompanyApp extends BackendApp
{
	var 	$s_mod;
	function __construct()
	{
		$this->ShipCompanyApp();
	}

	function ShipCompanyApp()
	{
		$this->s_mod=&m('shippingline');
		parent::BackendApp();

	}


	function index()
	{
		$db=&db();

		$zhdm_name=empty($_GET['zhdm_name'])?'':$_GET['zhdm_name'];
		$conditions = "1=1 ";
		if($zhdm_name){
			$conditions.=" and (code like '%".$zhdm_name."%' or sname like '%".$zhdm_name."%')";
		}

		$this->import_resource(array('script' => 'inline_edit.js'));

		$page   =   $this->_get_page(10);   //获取分页信息

		$sql="select * from  zys_shippingline where $conditions order by sort_order asc,add_time desc limit $page[limit]";
		$sc=$db->getAll($sql);

		$sql="select count(1) from  zys_shippingline where $conditions";
		$page['item_count']=$db->getOne($sql);;   //获取统计数据

		$this->_format_page($page);
		$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
		$this->assign('sc', $sc);
		$this->assign('tabself', json_encode(array('text'=>'船公司管理')));
		$this->display('shipcompany.index.html');
	}



	function edit()
	{
		$sid = empty($_GET['id']) ? 0 : $_GET['id'];
		if (!$sid)
		{
			$this->show_warning('船公司不存在','','index.php?app=shipcompany&act=edit&id='. $sid);
			return;
		}
		$sc = $this->s_mod->get_info($sid);
		if (!$sc)
		{
			$this->show_warning('船公司不存在','','index.php?app=shipcompany&act=edit&id='. $sid);
			return;
		}
		if (!IS_POST)
		{
			$template_name = $this->_get_template_name();
			$style_name    = $this->_get_style_name();
			$this->import_resource(array('script' => 'jquery.plugins/jquery.validate.js,change_upload.js'));
			$this->assign('build_editor', $this->_build_editor(array(
                'name' => 'description',
                'content_css' => SITE_URL . "/themes/mall/{$template_name}/styles/{$style_name}/css/ecmall.css"
			)));

			$this->assign('sc', $sc);
			$this->assign('tabself', json_encode(array('text'=>'编辑船公司')));
			$this->assign('tabback', json_encode(array('text'=>'船公司管理','url'=>'')));

			$this->display('shipcompany.form.html');
		}
		else
		{
			$db=&db();

			$code=$_POST['code'];
			if(empty($code)){
				$this->show_warning('编码不能为空','','index.php?app=shipcompany&act=edit&id='. $sid);
				return;
			}
				
			$sql="select 1 from zys_shippingline where code='".$code."' and sid<>".$sid;
			$row=$db->getOne($sql);
			if($row){
				$this->show_warning('编码已经存在，换个编码吧','','index.php?app=shipcompany&act=edit&id='. $sid);
				return;
			}

			$data = array(
                'sname' => trim($_POST['sname']),
                'sort_order'  => trim($_POST['sort_order']),
                'description'  => trim($_POST['description']),
                'brief' => $_POST['brief'],
			 	'code' => $_POST['code'],
				'isuse' => intval($_POST['isuse'])
			);

			if (!empty($_FILES['portrait']['tmp_name']))
			{
				$portrait = $this->_upload_portrait($sid);

				$portrait && $this->s_mod->edit($sid, array('logoimg' => $portrait));
			}




			$rows=$this->s_mod->edit($sid,$data);
			if ($this->s_mod->has_error())
			{
				$this->show_warning('修改失败','','index.php?app=shipcompany&act=edit&id='. $sid);
				return;
			}


			$this->show_message('修改成功',
                '', 'index.php?app=shipcompany&act=edit&id=' . $sid);
		}
	}

	//添加
	function add()
	{
		if (!IS_POST)
		{

			$sc=array();
			$sc['isuse']=1;
			$sc['sort_order']=255;

			$template_name = $this->_get_template_name();
			$style_name    = $this->_get_style_name();
			$this->import_resource(array('script' => 'jquery.plugins/jquery.validate.js,change_upload.js'));
			$this->assign('build_editor', $this->_build_editor(array(
                'name' => 'description',
                'content_css' => SITE_URL . "/themes/mall/{$template_name}/styles/{$style_name}/css/ecmall.css"
			)));

			$this->assign('sc', $sc);
			$this->assign('tabself', json_encode(array('text'=>'编辑船公司')));
			$this->assign('tabback', json_encode(array('text'=>'船公司管理','url'=>'')));

			$this->display('shipcompany.form.html');
		}
		else
		{
				
			$db=&db();

			$code=$_POST['code'];
			if(empty($code)){
				$this->show_warning('编码不能为空','','index.php?app=shipcompany&act=add');
				return;
			}
				
			$sql="select 1 from zys_shippingline where code='".$code."'";
			$row=$db->getOne($sql);
			if($row){
				$this->show_warning('编码已经存在，换个编码吧','','index.php?app=shipcompany&act=add');
				return;
			}
				
			$data = array(
                'sname' => trim($_POST['sname']),
                'sort_order'  => trim($_POST['sort_order']),
                'description'  => trim($_POST['description']),
                'brief' => $_POST['brief'],
				'code' => $code,
				'add_time' =>gmtime(),
				'isuse' => intval($_POST['isuse'])
			);

			$sid=$this->s_mod->add($data);
			if ($this->s_mod->has_error())
			{
				$this->show_warning('添加失败','','index.php?app=shipcompany&act=add');
				return;
			}


			if (!empty($_FILES['portrait']['tmp_name']))
			{
				$portrait = $this->_upload_portrait($sid);

				$portrait && $this->s_mod->edit($sid, array('logoimg' => $portrait));
			}

			$this->show_message('添加成功',
                '', 'index.php?app=shipcompany&act=edit&id=' . $sid);
		}
	}

	/**
	 * 上传头像
	 *
	 * @param int $user_id
	 * @return mix false表示上传失败,空串表示没有上传,string表示上传文件地址
	 */
	function _upload_portrait($user_id)
	{
		$file = $_FILES['portrait'];
		if ($file['error'] != UPLOAD_ERR_OK)
		{
			return '';
		}
		if ($file['error'] == UPLOAD_ERR_NO_FILE)
		{
			return '';
		}

		import('uploader.lib');
		$uploader = new Uploader();
		$uploader->allowed_type(IMAGE_FILE_TYPE);
		$uploader->addFile($file);
		if ($uploader->file_info() === false)
		{
			$this->show_warning($uploader->get_error(), '', 'index.php?app=shipcompany&amp;act=edit&amp;id=' . $user_id);
			return false;
		}

		$uploader->root_dir(ROOT_PATH);
		return $uploader->save('data/files/company/logo_'.$user_id, gmtime());
	}

	//异步修改数据
	function ajax_col()
	{
		$id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$column = empty($_GET['column']) ? '' : trim($_GET['column']);
		$value  = isset($_GET['value']) ? trim($_GET['value']) : '';
		$data   = array();

		if (in_array($column ,array('isuse','isuse1','isuse2','sort_order')))
		{
			$data[$column] = $value;
			$this->s_mod->edit($id, $data);
			if(!$this->s_mod->has_error())
			{
				echo ecm_json_encode(true);
			}
		}
		else
		{
			return ;
		}
		return ;
	}


	//删除
	function drop(){
		$id = isset($_GET['id']) ? trim($_GET['id']) : '';
		if (!$id)
		{
			$this->show_warning('请选中船公司后再删除');
			return;
		}

		$ids = explode(',', $id);

		/* 连接用户系统，从用户系统中删除会员 */
		if (!$this->s_mod->drop($ids))
		{
			$this->show_warning($this->s_mod->get_error(),'','index.php?app=shipcompany');

			return;
		}

		$this->show_message('删除成功','','index.php?app=shipcompany');
	}
}